library(tidyverse)
## -- Attaching packages -------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.1     v purrr   0.3.2
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   1.0.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts ----------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.4-7, (SVN revision 845)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/fmwil/OneDrive/Documents/R/win-library/3.6/rgdal/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/fmwil/OneDrive/Documents/R/win-library/3.6/rgdal/proj
##  Linking to sp version: 1.3-2
library(maps)
## 
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
## 
##     map
library(devtools)
## Loading required package: usethis
library(leaflet)
library(maptools)
## Checking rgeos availability: FALSE
##      Note: when rgeos is not available, polygon geometry     computations in maptools depend on gpclib,
##      which has a restricted licence. It is disabled by default;
##      to enable gpclib, type gpclibPermit()
library(BAMMtools)
## Loading required package: ape
census_tracts = readOGR(dsn = "mapping_files/censustracts2010.shp", encoding = "UTF-8") 
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\Users\fmwil\OneDrive\Documents\Mailman\Semester 3\Data Science\Final Project\EvictionsFinalProject\mapping_files\censustracts2010.shp", layer: "censustracts2010"
## with 2165 features
## It has 11 fields
census_tracts@data = census_tracts@data %>% 
  mutate(
    county_code = as.character(recode(boro_code, "1" = "061", "2" = "005", "3" = "047", "4" = "081", "5" = "085")),
    ct2010 = as.character(ct2010),
    fips_11dig = paste0("36", county_code, ct2010))

#loading eviction data
eviction_data = read_csv("data/EvictionData_NY.csv") %>%
  janitor::clean_names() %>% 
  mutate(geoid = as.character(geoid)) %>% 
  filter(year == 2000)
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   `parent-location` = col_character()
## )
## See spec(...) for full column specifications.
census_tracts@data = left_join(census_tracts@data, eviction_data, by = c("fips_11dig" = "geoid"))
census_tracts_crs = spTransform(census_tracts, CRS("+init=epsg:4326"))

writeOGR(census_tracts_crs, './mapping_files/census_tracts_geojson', layer = 'census_tracts', driver = 'GeoJSON')
#pop up label
label_popup = paste0(
  "<strong>Census tract: </strong>",
  census_tracts$ct2010,
  "<br><strong>Eviction rate: </strong>",
  census_tracts$eviction_rate
)

# get jenks natural breaks -- 5?????
getJenksBreaks(census_tracts$eviction_rate, 8)
## [1]   0.00   3.29  15.29  42.02 100.00 210.08 300.00 600.00
#set bins based on natural breaks
eviction_rate_bins = c(0, 3, 15, 42, 100, 210, 300, 600)

#set color palette
eviction_palette = colorBin('Greens', bins = eviction_rate_bins, na.color = '#aaff56')

Creating choropleth

leaflet::leaflet(data = census_tracts_crs) %>% 
  addProviderTiles('CartoDB.Positron') %>% 
  addPolygons(
    fillColor = ~eviction_palette(eviction_rate),
    fillOpacity = 0.8,
    color = "BDBDC3",
    weight = 1,
    popup = label_popup,
    highlightOptions = highlightOptions(color = "black", weight = 2, bringToFront = TRUE)) %>% 
      addLegend('bottomleft',
                pal = eviction_palette,
                values = ~eviction_rate,
                title = 'Eviction rates in NYC by census tract',
                opacity = 1)